package com.app.car.bean;

import com.alibaba.fastjson.annotation.JSONField;
import com.app.car.basis.Constants;
import com.app.car.greendao.GreenDaoUtils;

import my.FileUtils;
import my.LogUtil;
import my.TimeUtils;

public class BaseInfoUpdateCache extends BasisBean {


    private boolean needUpdateDevice = true;    //设备
    private boolean needUpdateGoodsType = true;    //货品种类


    private int deviceCount;
    private int goodsTypeCount;



    private int deviceCountUpdated;
    private int goodsTypeCountUpdated;


    private boolean isForceUpdate = false;


    @JSONField(serialize = false)
    public boolean isNeedUpdate(){
        if(isForceUpdate )return true;
        LogUtil.i("-----time111---"+ TimeUtils.getCurrentTimeByFormat("HH:mm:ss"));

        if( needUpdateDevice ){
            needUpdateDevice = (deviceCount > 0);
        }
        if( needUpdateGoodsType ){
            needUpdateGoodsType = (goodsTypeCount > 0);
        }


        LogUtil.i("-----time222---"+ TimeUtils.getCurrentTimeByFormat("HH:mm:ss"));
        if(needUpdateGoodsType || needUpdateDevice)return true;

            return false;
    }

    @JSONField(serialize = false)
    public void setUpdateCountByType(String map_type,int  count){
        switch (map_type){
            case GreenDaoUtils.TYPE_GOODS:
                goodsTypeCount = count;
                break;
            case GreenDaoUtils.TYPE_DEVICE:
                deviceCount = count;
                break;

        }
    }

    @JSONField(serialize = false)
    public boolean isUpdateSuccess(){
        if((goodsTypeCountUpdated == 0) && (deviceCountUpdated == 0)){
            return false;
        }
        return true;
    }


    @JSONField(serialize = false)
    public void setUpdatedCountByMaptype(String type,int updatedCount){
        switch (type){
            case GreenDaoUtils.TYPE_DEVICE:
                deviceCountUpdated = updatedCount;
                break;
            case GreenDaoUtils.TYPE_GOODS:
                goodsTypeCountUpdated = updatedCount;
                break;

        }
    }


    @JSONField(serialize = false)
    public void setAllNeedUpdate(boolean isNeed){
        needUpdateDevice = isNeed;
        needUpdateGoodsType = isNeed;
    }


    public static final String FILE_CACHE = Constants.DIR_FILECACHE
            + "tea_ware_baseinfo_update_cache_.data";

    @JSONField(serialize = false)
    public static BaseInfoUpdateCache getFromCache() {
        BaseInfoUpdateCache data = (BaseInfoUpdateCache) FileUtils.readObject(FILE_CACHE);
        if (data == null) {
            data = new BaseInfoUpdateCache();
        }
        return data;
    }

    @JSONField(serialize = false)
    public void save() {
        isForceUpdate =false;
        FileUtils.cacheObject(FILE_CACHE, this);
    }
    @JSONField(serialize = false)
    public static void delete() {
        FileUtils.cacheObject(FILE_CACHE, null);
    }


    public boolean isNeedUpdateDevice() {
        return needUpdateDevice;
    }

    public void setNeedUpdateDevice(boolean needUpdateDevice) {
        this.needUpdateDevice = needUpdateDevice;
    }

    public int getDeviceCount() {
        return deviceCount;
    }

    public void setDeviceCount(int deviceCount) {
        this.deviceCount = deviceCount;
    }

    public int getDeviceCountUpdated() {
        return deviceCountUpdated;
    }

    public void setDeviceCountUpdated(int deviceCountUpdated) {
        this.deviceCountUpdated = deviceCountUpdated;
    }



    public boolean isForceUpdate() {
        return isForceUpdate;
    }

    public void setForceUpdate(boolean forceUpdate) {
        isForceUpdate = forceUpdate;
    }

    public boolean isNeedUpdateGoodsType() {
        return needUpdateGoodsType;
    }

    public void setNeedUpdateGoodsType(boolean needUpdateGoodsType) {
        this.needUpdateGoodsType = needUpdateGoodsType;
    }

    public int getGoodsTypeCount() {
        return goodsTypeCount;
    }

    public void setGoodsTypeCount(int goodsTypeCount) {
        this.goodsTypeCount = goodsTypeCount;
    }

    public int getGoodsTypeCountUpdated() {
        return goodsTypeCountUpdated;
    }

    public void setGoodsTypeCountUpdated(int goodsTypeCountUpdated) {
        this.goodsTypeCountUpdated = goodsTypeCountUpdated;
    }

}
